<template>
<center>
  <div>
    <h1>自律一下</h1>
    <input v-model="newTodoTitle" placeholder="输入待办事项">
    <button @click="addTodo">添加</button>
    <ul style="list-style-type: none;">
      <li v-for="(todo, index) in todos" :key="index">
        <TodoItem :title="todo.title" :completed="todo.completed" @update:completed="updateTodoCompleted" @remove-todo="removeTodo(index)"></TodoItem>
      </li>
    </ul>
  </div>
  </center>
</template>

<script>
import TodoItem from './TodoItem.vue';

export default {
  components: {
    TodoItem
  },
  data() {
    return {
      newTodoTitle: '',
      todos: []
    };
  },
  methods: {
    addTodo() {
      if (this.newTodoTitle.trim()) {
        this.todos.push({ title: this.newTodoTitle, completed: false });
        this.newTodoTitle = '';
      }
    },
    removeTodo(index) {
      this.todos.splice(index, 1);
    },
    updateTodoCompleted(index, completed) {
      this.todos[index].completed = completed;
    }
  }
}
</script>
